<template>
  <div class="gm-detail">
    <van-nav-bar title="标题" class="title"
      ><template #left> <p @click="$router.back()">返回</p> </template>
    </van-nav-bar>
    <div class="center">
      <h2>{{ this.data.stem }}</h2>
      <span>{{ this.data.createdAt }}</span>
      <span>|</span>
      <span>{{ this.data.likeCount }} 浏览量</span>
      <span>|</span>
      <span>{{ this.data.views }} 评论</span>
      <div class="row">
        <img :src="this.data.avatar" alt="" />
        <div>{{ this.data.creator }}</div>
      </div>
      <div class="content" v-html="data.content"></div>
    </div>
    <div class="icon-list">
      <div class="icon">
        <van-icon
          name="star-o"
          size="50"
          v-if="this.data.collectFlag === 0"
          @click="changeLike(2)"
        />
        <van-icon name="star" size="50" v-else @click="changeLike(2)" />
      </div>
      <div class="icon">
        <van-icon
          name="like-o"
          size="50"
          v-if="this.data.likeFlag === 0"
          @click="changeLike(1)"
        />
        <van-icon name="like" size="50" v-else @click="changeLike(1)" />
      </div>
    </div>
  </div>
</template>
<script>
import axios from "axios";

export default {
  name: "HmDetail",
  async created() {
    await this.getData();
  },
  data() {
    return {
      data: {
        likeFlag: 0,
        collectFlag: 0,
      },
    };
  },
  methods: {
    async changeLike(v) {
      await axios({
        url: "http://interview-api-t.itheima.net/h5/interview/opt",
        method: "post",
        data: {
          id: this.$route.params.id.toString(),
          optType: v,
        },
        headers: {
          Authorization: `Bearer ${localStorage.getItem("token")}`,
        },
      });
      this.getData();
    },
    async getData() {
      const res = await axios({
        url: `http://interview-api-t.itheima.net/h5/interview/show?id=${this.$route.params.id}`,
        headers: {
          Authorization: `Bearer ${localStorage.getItem("token")}`,
        },
      });
      this.data = res.data.data;
    },
  },
};
</script>
<style lang="less" scoped>
.gm-detail {
  .title {
    height: 50px;
    width: 100%;
    position: fixed;
    top: 0;
    p {
      color: orange;
    }
  }
  .center {
    margin-top: 50px;
    padding: 0 10px;
    span {
      color: #a8a8a8;
      font-size: 14px;
      align-items: center;
      line-height: 100%;
    }
    .row {
      padding: 10px 10px;
      display: flex;
      img {
        width: 25px;
        align-items: center;
      }
      div {
        color: #a8a8a8;
        font-size: 14px;
        margin-left: 10px;
        margin-top: 2px;
      }
    }
  }
}
.icon-list {
  position: fixed;
  bottom: 100px;
  right: 50px;
  display: flex;
  justify-content: space-between;
  width: 150px;
  .icon {
    width: 50px;
    height: 50px;
    background-color: #fff;
    border-radius: 50%;
    //加阴影
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
  }
}
</style>
